package com.java.controller.admin;

import com.github.pagehelper.PageInfo;
import com.java.service.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private PlayerService playerService;

    /**
     * 做一个登录Controller
     * 账号密码设置为静态
     *账号：admin
     *密码：yunduanb203
     */
    @RequestMapping("/intoAdmin")
    public String intoAdmin(String userName,String password){
        if("admin".equals(userName)&&"yunduanb203".equals(password)){
            return "admin/adminOperation.jsp";
        }else {
            return "error/error.jsp";
        }
    }

    /**
     * 查询所有队信息，并进行分页显示
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("/getAllPlayers")
    public String getAllPlayer(@RequestParam(defaultValue = "1") int pageNum,
                               @RequestParam(defaultValue = "2") int pageSize,
                               Model model){
        List<Map<String, Object>> playerList = playerService.findAllPlayer(pageNum, pageSize);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(playerList);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/showAllGroups.jsp";
    }

    /**
     * 参赛选手查询本队信息，以队长手机号查询
     */
    @RequestMapping("/getOneTeam")
    public String getPlayer(String phone,Model model){
        Map<String,Object> map = new HashMap<>();
        map.put("phone",phone);
        Map<String, Object> oneTeam = playerService.findOnePlayer(map);
        model.addAttribute("oneTeam",oneTeam);
        return "admin/yourTeamInfo.jsp";
    }

    /**
     * 修改团队信息
     */
    @RequestMapping("/updateTeamInfo")
    public @ResponseBody String updateTeamInfo(@RequestParam Map<String,Object> map){
        //把手机号取出来
        String captain_phone = (String) map.get("captain_phone");
        int updateResult = playerService.updatePlayer(map);
        if(updateResult==1){
            return "forward:/admin/getOneTeam.do?phone="+captain_phone;
        }else {
            return "error/error.jsp";
        }
    }
}
